﻿using Syntax;
using static Syntax.Syntax10;
Console.WriteLine("Hello, World!");
User user = new User() { FirstName = "严", LastName = "浩淼" };
LimitedName limitedName = user;//将user转换为limitedName
string lName = limitedName;//将limitedName转换为字符串型
int lCount = limitedName;
Console.WriteLine($"名字:{lName}");
Console.WriteLine($"长度:{lCount}");

public class LimitedName
{
    const int MaxNameLength = 10;//名字最长为10个字符
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value.Length < 10 ? value : value.Substring(0, 10); }
    }
    // public static implicit operator是必须的，名称LimitedName为目标类型，参数User为源数据。
    public static implicit operator LimitedName(User user)
    {
        LimitedName ln = new LimitedName();//建立目标实例
        ln.Name = user.FirstName + user.LastName;//将源数据赋于目标实例
        return ln;
    }
    public static implicit operator string(LimitedName ln) => ln.Name;

    public static implicit operator int(LimitedName ln) => ln.Name.Length;
}
public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
